package com.tedu.charging.service.cost.service.impl;

import com.tedu.charging.common.pojo.param.ChargingProgressParam;
import com.tedu.charging.common.pojo.vo.ChargeProgressVO;
import com.tedu.charging.service.cost.CostInit;
import com.tedu.charging.common.pojo.bo.ChargeData;
import com.tedu.charging.service.cost.pojo.po.ChargingCostRule;
import com.tedu.charging.service.cost.service.ICostService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CostServiceImpl implements ICostService {
    //设备每次上传充电过程数据后，要立刻计算总费用和时长
    @Override
    public ChargeProgressVO computationalCost(ChargingProgressParam chargingProgressParam) {
        String billId = chargingProgressParam.getBillId();
        //取以前充电数据
        ChargeData chargeData = CostInit.billChargingDataConcurrentHashMap.get(billId);
        //没取到说明是第一次上传充电数据
        if (ObjectUtils.isEmpty(chargeData)) {
            //创建实体类
            chargeData = new ChargeData();
            chargeData.setBillId(billId);
            chargeData.setUserId(chargingProgressParam.getUserId());
            chargeData.setStartTime(LocalDateTime.now());

            //保存到hashmap中
            CostInit.billChargingDataConcurrentHashMap.put(billId, chargeData);
        }
        chargeData.setGunId(chargingProgressParam.getGunId());

//判断时段
        //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //LocalDateTime localDateTime = LocalDateTime.parse(chargingProgressParam.getCreateTime(), formatter);
        LocalDateTime localDateTime = LocalDateTime.now();
        //取充电时的小时
        int hour = localDateTime.getHour();
        //map中用两个key保存一个充电站的计费规则
        //1-1保存编号为1充电场站的慢充计费规则
        //1-2保存编号为2充电场站的快充计费规则
        String key = chargingProgressParam.getStationId() + "-" + chargingProgressParam.getGunType();
//取充电站的计费规则
        List<ChargingCostRule> stationCostRuleList = CostInit.stationCostConcurrentHashMap.get(key);

        for (ChargingCostRule chargingCostRule : stationCostRuleList) {

            //判断充电是在那个时段
            if (hour >= chargingCostRule.getStartTime() && hour < chargingCostRule.getEndTime()) {
                //取电价
                BigDecimal cost = chargingCostRule.getPowerFee();
                chargeData.setOneElectricityCost(cost);

                //取电量
                //判断是不是第一次上传电量
                if (chargeData.getSecondToLastChargingProgressParam() == null) {
                    //第一次上传电量
                    chargeData.setLastChargingProgressParam(chargingProgressParam);
                    BigDecimal capacity = new BigDecimal(Float.toString(chargingProgressParam.getChargingCapacity()));
                    BigDecimal chargingCost = cost.multiply(capacity);
                    chargeData.setTotalCost(chargingCost);
                    chargeData.setSecondToLastChargingProgressParam(chargingProgressParam);
                    chargeData.setLastChargingProgressParam(chargingProgressParam);
                } else {
                    //取上一次最新充电数据
                    ChargingProgressParam secondToLastChargingProgressParam = chargeData.getLastChargingProgressParam();
                    //保存成第二次
                    chargeData.setSecondToLastChargingProgressParam(secondToLastChargingProgressParam);

                    //保存最新充电数据
                    chargeData.setLastChargingProgressParam(chargingProgressParam);

                    //取本次充电电量
                    float capacity = chargingProgressParam.getChargingCapacity() - secondToLastChargingProgressParam.getChargingCapacity();
                    BigDecimal chargingCost = cost.multiply(new BigDecimal(Float.toString(capacity)));
                    //电费累加
                    BigDecimal totalCost = chargeData.getTotalCost().add(chargingCost);
                    chargeData.setTotalCost(totalCost);
                }

            }
        }

        //计算时长
        Duration duration = Duration.between(chargeData.getStartTime(), LocalDateTime.now());

        long hours = duration.toHours();
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;

        //创建返回的vo
        ChargeProgressVO chargeProgressVO = new ChargeProgressVO();
        BeanUtils.copyProperties(chargingProgressParam, chargeProgressVO);
        chargeProgressVO.setOneElectricityCost(chargeData.getOneElectricityCost());

        chargeProgressVO.setHours(hours);
        chargeProgressVO.setMinutes(minutes);
        chargeProgressVO.setSeconds(seconds);

        chargeProgressVO.setTotalCost(chargeData.getTotalCost());

        return chargeProgressVO;
    }

    @Override
    public ChargeProgressVO getCost(String billId) {
        //获取充电过程数据
        ChargeData chargeData = CostInit.billChargingDataConcurrentHashMap.get(billId);

        //创建用于返回的vo
        ChargeProgressVO chargeProgressVO = new ChargeProgressVO();
        BeanUtils.copyProperties(chargeData, chargeProgressVO);

        chargeProgressVO.setChargingCapacity(chargeData.getLastChargingProgressParam().getChargingCapacity());
        chargeProgressVO.setTotalCapacity(chargeData.getLastChargingProgressParam().getTotalCapacity());

        //计算最新时长
        Duration duration = Duration.between(chargeData.getStartTime(), LocalDateTime.now());

        long hours = duration.toHours();
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;
        chargeProgressVO.setHours(hours);
        chargeProgressVO.setMinutes(minutes);
        chargeProgressVO.setSeconds(seconds);

        return chargeProgressVO;
    }
}
